package com.yndsoft.jc.ui.memberpage.address;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.join.PopupWindowAddress;
import com.yndsoft.jc.supplier.net.util.ProgressSubsc;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.supplier.widget.dialog.SupplierDialog;
import com.yndsoft.jc.supplier.widget.popupwindow.OnChooseDataListener;
import com.yndsoft.jc.ui.memberpage.myorder.MemberAddress;

import butterknife.Bind;

/**
 * Created by hasee on 2017/12/7.
 */

public class AddMailAddressActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.base_title)
    TextView base_title;

    @Bind(R.id.add_name)
    EditText add_name;
    @Bind(R.id.add_cell)
    EditText add_cell;
    @Bind(R.id.add_address_detail)
    EditText add_address_detail;
    @Bind(R.id.set_default)
    CheckBox set_default;
    @Bind(R.id.add_complete)
    Button add_complete;

    @Bind(R.id.site)
    TextView site;
    @Bind(R.id.site_change)
    RelativeLayout site_change;
    @Bind(R.id.address_root)
    LinearLayout address_root;

    boolean isChoose;
    private int provinceKey = 0;
    private String provinceValue = "";
    private int cityKey = 0;
    private String cityValue = "";
    private int districtKey = 0;
    private String districtValue = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_mail_address);

        setViews();
        setListener();
    }

    private void setViews() {
        base_title.setText(getResources().getString(R.string.add_mail_address));
    }

    private void setListener() {
        base_backBtn.setOnClickListener(this);
        add_complete.setOnClickListener(this);
        site_change.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
            case R.id.add_complete:
                if (TextUtils.isEmpty(add_name.getText())) {
                    showHint("送检人不能为空");
                } else {
                    if (add_cell.getText().length() != 11) {
                        showHint("请输入正确的手机号");
                    } else {
                        if (TextUtils.isEmpty(add_address_detail.getText())) {
                            showHint("输入详细地址");
                        } else {
                            setDatas();
                        }
                    }
                }
                break;
            case R.id.site_change:
                PopupWindowAddress address = new PopupWindowAddress(mContext, site.getText().toString());
                address.showAtLocation(address_root, Gravity.BOTTOM, 0, 0);
                address.setOnChooseDataListener(new OnChooseDataListener() {
                    @Override
                    public void onChooseData(int key1, String value1, int key2, String value2, int key3, String value3) {
                        site.setText(value1 + " " + value2 + " " + value3);
                        site.setTextColor(0xFF333333);
                        provinceKey = key1;
                        provinceValue = value1;
                        cityKey = key2;
                        cityValue = value2;
                        districtKey = key3;
                        districtValue = value3;
                    }
                });
                break;
        }
    }

    private void setDatas() {
        MemberAddress address = new MemberAddress();
        address.setName(add_name.getText().toString());
        address.setPhone(add_cell.getText().toString());
        address.setAddress(add_address_detail.getText().toString());
        address.setProvinceid(provinceKey);
        address.setProvince(provinceValue);
        address.setCityid(cityKey);
        address.setCity(cityValue);
        address.setDistrictid(districtKey);
        address.setDistrict(districtValue);
        if (set_default.isChecked()) {
            address.setIsdefault(true);
        } else {
            address.setIsdefault(false);
        }
        mHttpUtil.postMailAddress(new ProgressSubsc<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {

                switch (returnMsg.getCode()) {
                    case 0:
                        ToastUtil.show(mContext, "添加失败，请重试");
                        break;
                    case 1:
                        ToastUtil.show(mContext, "添加成功！");
                        finish();
                        break;
                }
            }
        }, "addaddress", mSession.getUserInfo().getId(), new Gson().toJson(address));
    }

    private void showHint(String content) {
        new SupplierDialog(mContext).show(false, "提示", content);
    }
}
